//
//  DetailViewController.swift
//  swift-02-demo
//
//  Created by China Dragon on 2023/8/21.
//

import UIKit
/*
 Xcode connection里只能是action灰色的，不能选outlet问题解决
 https://blog.csdn.net/qq_45673372/article/details/131068275
 */
class DetailViewController: UIViewController {
    //定义闭包属性
    var didSaveCallBack:(() ->() )?
    
    
    //个人数据模型
    var person: Person?
    
    @IBOutlet weak var nameText: UITextField!
    
    @IBOutlet weak var ageText: UITextField!
    
    @IBAction func save(_ sender: Any) {
        //使用UI 更新模型
        person?.name = nameText.text
        person?.age = Int( ageText.text!) ?? 0
        
        //完成回调，通知控制器更新数据 - 闭包
        didSaveCallBack?()
        
        //关闭控制器
        navigationController?.popViewController(animated: true)
        
    }
    
    @IBAction func textchange() {
        //两个都有值才能保存
        navigationItem.rightBarButtonItem?.isEnabled = nameText.hasText && ageText.hasText
        
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        
        nameText.text = person?.name
        ageText.text = "\(person?.age ?? 0)"
        textchange()

    }
    
}
